package guiobjects;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Draws an observation.
 * 
 * @author Trevor Terris
 *
 */

public class Observation implements IPaintedObject
{
	public static final int numAttributes = 4;
	public int frameNum;
	public double x;
	public double z;
	public int id;
	public int camera = -1;
	public double[] attributes = new double[numAttributes];
	public boolean isEntranceNode = false;
	public boolean isExitNode = false;
	public int dummyOf;
	
	public Observation()
	{
		frameNum = 0;
		x = 0.0;
		z = 0.0;
		id = 0;
	}
	
	public Observation(Observation i)
	{
		frameNum = i.frameNum;
		x = i.x;
		z = i.z;
		id = i.id;
		camera = i.camera;
		attributes = new double[i.attributes.length];
		for (int j = 0; j < attributes.length; j++)
			attributes[j] = i.attributes[j];
		isEntranceNode = i.isEntranceNode;
		isExitNode = i.isExitNode;
		dummyOf = i.dummyOf;
	}
	
	public Observation(String input)
	{
		parseText(input);
	}
	
	public String toString()
	{
		String toReturn = "Observation " + frameNum + " " + x + " " + z + " " ;
		
		for (int i = 0; i < attributes.length; i++)
			toReturn += attributes[i] + " ";
		toReturn += id + " " + camera;
		
		if (isEntranceNode)
		{
			toReturn += " leads " + dummyOf + "\n";
		}
		else if (isExitNode)
		{
			toReturn += " follows " + dummyOf + "\n";
		}
		else
		{
			toReturn += " self\n";
		}

		return toReturn;
	}
	
	public void parseText(String input)
	{
		Scanner s = new Scanner(input);
		x = s.nextDouble();
		z = s.nextDouble();
		ArrayList<Double> attributeArray = new ArrayList<Double>();
		while (s.hasNextDouble() && !s.hasNextInt())
			attributeArray.add(s.nextDouble());
		int size = attributeArray.size();
		attributes = new double[size];
		for (int k = 0; k < size; k++)
		{
			attributes[k] = attributeArray.get(k);
		}
		id = s.nextInt();
		camera = s.nextInt();
		String obType = s.next();
		if (obType.equals("leads"))
		{
			isEntranceNode = true;
			dummyOf = s.nextInt();
		}
		else if (obType.equals("follows"))
		{
			isExitNode = true;
			dummyOf = s.nextInt();
		}
	}
	
	public void drawObject(java.awt.Graphics pen, int screenWidth, int screenHeight, int offset)
	{
		if (!isEntranceNode && !isExitNode)
		{
	        int rValue = (attributes.length >= 1)?(int)(attributes[0] * 255):0;
	        int gValue = (attributes.length >= 2)?(int)(attributes[1] * 255):0;
	        int bValue = (attributes.length >= 3)?(int)(attributes[2] * 255):0;
	        int radius = (attributes.length >= 4)?(int)(attributes[3] * 30 + 10):10;
	        int xCorner = (int)(x * screenWidth - radius);
	        int zCorner = (int)(z * screenHeight - radius);
	        pen.setColor(new Color(rValue, gValue, bValue));
	        pen.fillOval(xCorner + offset, zCorner, radius * 2, radius * 2);
	        pen.setColor(Color.WHITE);
	        pen.drawString(Integer.toString(id), xCorner + radius - 5 + offset, zCorner + radius + 5);
		}
		else
		{
			pen.setColor(Color.black);
			double newZ = 0.0;
			double newX = 0.0;
			if (Math.min(1.0 - z, z) < Math.min(1.0-x, x))
			{
				newZ = (z < 1.0-z)?0.0:1.0;
				newX = x;
			}
			else
			{
				newX = (x < 1.0-x)?0.0:1.0;
				newZ = z;
			}
			pen.fillRect((int)(newX * screenWidth - 5) + offset, (int)(newZ * screenHeight - 5), 10, 10);
			pen.setColor(Color.white);
			pen.drawString(Integer.toString(id),(int)(newX * screenWidth - 2) + offset, (int)(newZ * screenHeight + 2));
		}
	}
	public String getSVGData(int width, int offset, int total)
	{
		if (!isEntranceNode && !isExitNode)
		{
	        int rValue = (attributes.length >= 1)?(int)(attributes[0] * 255):0;
	        int gValue = (attributes.length >= 2)?(int)(attributes[1] * 255):0;
	        int bValue = (attributes.length >= 3)?(int)(attributes[2] * 255):0;
	        int radius = (attributes.length >= 4)?(int)(attributes[3] * 30 + 10):10;
	
			String output = "<circle cx=\"";
			double xValue = (((double)offset + (double)width * x) * 100.0 / (double) total);
			
			output += xValue + "%\" cy=\"";
			double yValue = (z * 100.0);
			output += yValue + "%\" r=\"" + radius + "\" style=\"fill:rgb(" + rValue + "," + 
						gValue + "," + bValue + ");\"/>";
			
			output += "<text x=\""+xValue+"%\" y=\""+yValue+"%\" style=\"dominant-baseline: central;text-anchor: middle;fill:rgb(255,255,255);\">" 
						+ id + "</text>";
			return output;
		}
		else return "";
	}
}
 